import axios from 'axios';

const API_BASE_URL = 'http://localhost:8001/data/';

export interface StockMinuteData {
    time: string;
    price: number;
    vol: number;
    avg_price: number;
    pre_close: number;
}

export interface StockEnergyZtData {
    trade_date: string;
    stocks:StockZtData[];
}

export interface StockZtData {
    ts_code: string;
    name: string;
    close: number;
    pct_chg: number;  
    amount: number;
    turnover_rate_f: number;
    circ_mv: number;
}

export interface StockKData {
    trade_date: string;
    open: number;
    high: number;
    low: number;
    close: number;
    pct_chg: number;
    vol: number;
    amount: number;
    pre_close: number;
}

export interface StockInfoData {
    ts_code: string;
    name: number;
    close: number;
    pct_chg: number;
    amount: number;
    turnover_rate_f: number;
    volume_ratio: number;
    circ_mv: number;
    float_share: number;
    total_mv: number;
    total_share: number;
    industry: string;
    area: string;
    is_hs:string;
    pe:number;
    pb:number;
    dv_ratio:number;
}


export const fetchStockMinuteDataByCode = async (code: string,day:string): Promise<StockMinuteData[]> => {
    const params = {
        code: code,
        day: day
    }
    const response = await axios.get(`${API_BASE_URL}stock/tick/`,{params});
    return response.data;
};



export const fetchStockKDataByCode = async (code: string,period:string): Promise<StockKData[]> => {
    const params = {
        ts_code: code
    }
    if(period === 'day')
    {
        const response = await axios.get(`${API_BASE_URL}stock/kline/daily/`,{params});
        // console.log('fetchStockKDataByCode response',response.data)
        return response.data;
    }
    else if(period === 'week')
    {
        const response = await axios.get(`${API_BASE_URL}stock/kline/weekly/`,{params});
        return response.data;
    }
    else
    {
        const response = await axios.get(`${API_BASE_URL}stock/kline/monthly/`,{params});
        return response.data;
    }
};

export const fetchStockInfoByCode = async (code: string): Promise<StockInfoData> => {
    const params = {
        ts_code: code
    }
    const response = await axios.get(`${API_BASE_URL}stock/info/`,{params});
    // console.log('fetchStockInfoByCode response',response.data)
    return response.data;
};

// export const deleteStockMinuteDataByDate = async (date: string): Promise<void> => {
//     await axios.delete(`${API_BASE_URL}stock/tick/${date}/`);
// };


export const fetchStockEnergyZtData = async (): Promise<StockEnergyZtData> => {
    const response = await axios.get(`${API_BASE_URL}stock/energy-zt/`);
    // console.log('fetchStockEnergyZtData response',response)
    return response.data;
};